package com.ljfiction.book.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ljfiction.book.MyView.BadgeView;
import com.ljfiction.book.R;

import java.util.List;

public class SimpleFragmentMyauthorMessagePageAdapter extends FragmentPagerAdapter {
    private Context mContext;
    private List<Fragment> mFragmentList;
    private String[] mPageTitleList;
    private List<Integer> mBadgeCountList;

    public SimpleFragmentMyauthorMessagePageAdapter(Context context,
                                                    FragmentManager fm,
                                                    List<Fragment> fragmentList,
                                                    String[] pageTitleList,
                                                    List<Integer> badgeCountList) {
        super(fm);
        this.mContext = context;
        this.mFragmentList = fragmentList;
        this.mPageTitleList = pageTitleList;
        this.mBadgeCountList = badgeCountList;
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mPageTitleList[position];
    }

    public View getTabItemView(int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.tab_myauthormessage_item, null);
        TextView textView = (TextView) view.findViewById(R.id.tab_myauthormessage_textview);
        textView.setText(mPageTitleList[position]);
        View target = view.findViewById(R.id.tab_bag);
        BadgeView badgeView = new BadgeView(mContext);
        badgeView.setTargetView(target);
        badgeView.setBadgeMargin(0, 6, 10, 0);
        badgeView.setTextSize(10);
        badgeView.setText(formatBadgeNumber(mBadgeCountList.get(position)));

        return view;
    }


    public static String formatBadgeNumber(int value) {


        if (value < 100) {
            // equivalent to String#valueOf(int);
            return Integer.toString(value);
        }

        // my own policy
        return "99+";
    }
}
